var os      = require('os');
var cluster = require('cluster');
var cpuNum  = os.cpus().length;

var workerList = {};

if( cluster.isMaster ) {

    // 子进程意外退出后，自动重启
    cluster.on('exit', function ( worker ) {

        delete workerList[worker.pid];

        worker = cluster.fork();
        workerList[worker.pid] = worker;

    });

    // 按照CPU数量开启子进程
    for( var i = 0; i < cpuNum; i++ ) {
        var worker = cluster.fork();
        workerList[worker.pid] = worker;
    }

}
else {
    require('./index.js');
}